/*
 *  modelanim.cpp
 *  EncodeTest
 *
 *  Created by terry franguiadakis on 5/7/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "modelanim.h"
#include "bbmath.h"

// --------------------------------------------------------------------------
void Anim::Update(Model* i_model, float i_dt)
{
    if (m_utime <= 1.0f)
    {
        m_curtime = m_curtime + i_dt;
        m_utime   = m_curtime / m_duration;
    }
    else 
    {
        m_isDone = true;
    }
}

void AnimUV::Update(Model* i_model, float i_dt)
{
    Anim::Update(i_model, i_dt);
    m_curval.x = m_from.x * (1.0 - m_utime) + m_to.x * (m_utime);
    m_curval.y = m_from.y * (1.0 - m_utime) + m_to.y * (m_utime);
    i_model->SetUVOffset(getCurValue());
}

void AnimRot::Update(Model* i_model, float i_dt)
{
    Anim::Update(i_model, i_dt);
    // udpate anim
    m_curval = m_from * (1.0 - m_utime) + m_to * (m_utime);
    i_model->SetRot(getCurValue());
}

void AnimPos::Update(Model* i_model, float i_dt)
{
    Anim::Update(i_model, i_dt);
    m_curval.x = m_from.x * (1.0 - m_utime) + m_to.x * (m_utime);
    m_curval.y = m_from.y * (1.0 - m_utime) + m_to.y * (m_utime);
    
    i_model->SetPos(getCurValue());
}

void AnimScale::Update(Model* i_model, float i_dt)
{
    Anim::Update(i_model, i_dt);
    m_curval.x = m_from.x * (1.0 - m_utime) + m_to.x * (m_utime);
    m_curval.y = m_from.y * (1.0 - m_utime) + m_to.y * (m_utime);
    
    i_model->SetScale(getCurValue());
}

void AnimIndex::Update(Model* i_model, float i_dt)
{
    Anim::Update(i_model, i_dt);
//    m_curval = lerp 
    // BB (TEF) fix
 //   i_model.SetAtlasIndex(getCurValue());
}

// ----------------------------------------------------------

void Sequence::Update(Model* i_targetModel, float i_dt)
{
	/*
	struct UpdateAndDone 
	{
		UpdateAndDone(Model* i_targetModel, float i_dt):targetModel(i_targetModel), dt(i_dt) {}
			
		Model* targetModel;
		float  dt; 
		bool operator()(Anim* i_anim)
		{
			i_anim->Update(targetModel, dt);
			return i_anim->IsDone();
		}
	};
	
	m_anims.erase(std::remove_if(m_anims.begin(), m_anims.end(), UpdateAndDone(i_targetModel, i_dt));
				  
    for (int i = 0; toremove.size(); ++i)
    {
		// BB (tef) hmm there's probably a better way.
		std::vector<Anim*>::iterator iter = std::find(m_anims.begin(), m_anims.end(), toremove[i]);
        m_anims.erase(iter);
        m_inactiveanims.push_back(toremove[i]);
    }
	 */
}

SequenceSet* SequenceSet::Create(const std::vector<ModelData::Sequence*> &i_sequence)
{
	SequenceSet* newSet = new SequenceSet;
	
}
// ----------------------------------------------
Model* Model::Create(const ModelData::Model& i_modelResource)
{
	Model* model = new Model();
	
	model->m_pos.Set(i_modelResource.pos[0], i_modelResource.pos[1]);
	model->m_scale.Set(i_modelResource.scale[0], i_modelResource.scale[1]);
	model->m_rot = i_modelResource.rot;
	
	for (int ichild = 0; ichild < i_modelResource.children.size(); ++ichild)
	{
		Model* childModel = Model::Create(i_modelResource.children[ichild]);
		if (childModel)
		{
			model->push_back(childModel);
		}
		else
		{
			// ERROR - could not created required  child.
		}
	}
	
	
}